package teatime.core.service;

import java.util.List;
import java.util.Map;
import java.io.*;
import java.util.*;

import net.sf.json.JSONArray;

import org.dom4j.*;
import org.dom4j.io.*;
import org.springframework.util.ResourceUtils;

import teatime.core.service.manager.XmlManager;



public final class XmlManagerImpl implements XmlManager {

	private List<Map<String, Object>> menuList;
	
	private List<Map<String, Object>> CRMMenuList;

	private List<Map<String, Object>> areaList;

	public List<Map<String, Object>> getAreaList()
			throws FileNotFoundException, DocumentException {
		if (null == areaList) {
			setAreaList(getXmlList("classpath:area.xml"));
		}
		return areaList;
	}

	public void setAreaList(List<Map<String, Object>> areaList) {
		this.areaList = areaList;
	}

	private void setMenuList(List<Map<String, Object>> menuList) {
		this.menuList = menuList;
	}

	private void setCRMMenuList(List<Map<String, Object>> CRMMenuList) {
		this.CRMMenuList = CRMMenuList;
	}
	
	/**
	 * 获取主菜单列表
	 * @return 主菜单List
	 * @throws DocumentException
	 * @throws FileNotFoundException
	 */
	@SuppressWarnings("unchecked")
	public List<Map<String, Object>> getMenuList() throws DocumentException,
			FileNotFoundException {
		if (null == menuList) {
			setMenuList(getXmlList("classpath:menu.xml"));
		}
		return menuList;
	}
	
	/**
	 * 获取客户关系管理菜单列表
	 * @return 客户关系管理菜单List
	 * @throws DocumentException
	 * @throws FileNotFoundException
	 */
	@SuppressWarnings("unchecked")
	public List<Map<String, Object>> getCRMMenuList() throws DocumentException,
			FileNotFoundException {
		if (null == CRMMenuList) {
			setCRMMenuList(getXmlList("classpath:crm-menu.xml"));
		}
		return CRMMenuList;
	}

	private List<Map<String, Object>> getXmlList(String xmlFileName)
			throws FileNotFoundException, DocumentException {
		File f = ResourceUtils.getFile(xmlFileName);
		SAXReader reader = new SAXReader();
		Document doc = reader.read(f);
		Element root = doc.getRootElement();

		return getXmlList(root);
	}

	@SuppressWarnings("unchecked")
	private List<Map<String, Object>> getXmlList(Element e) {
		List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();

		for (Iterator<Element> it = e.elementIterator("item"); it.hasNext();) {
			Map<String, Object> xmlContent = new HashMap<String, Object>();
			Element foo = it.next();
			xmlContent.put("id", foo.attributeValue("id"));
			xmlContent.put("text", foo.attributeValue("text"));
			xmlContent.put("leaf", Boolean.valueOf(foo.attributeValue("leaf")));
			xmlContent.put("url", foo.attributeValue("url"));
			// 递归解析XML
			if (foo.elements().size() > 0) {
				xmlContent.put("children", getXmlList(foo));
			}
			list.add(xmlContent);
		}
		return list;
	}

	/**
	 * 通过异步方式获取主菜单列表
	 * @return 主菜单JSONArray
	 * @throws DocumentException
	 * @throws FileNotFoundException
	 */
	public JSONArray getMenuListByJSON() throws DocumentException,
			FileNotFoundException {
		return JSONArray.fromObject(getMenuList());
	}
	
	/**
	 * 通过异步方式获取客户关系管理菜单列表
	 * @return 客户关系管理菜单JSONArray
	 * @throws DocumentException
	 * @throws FileNotFoundException
	 */
	public JSONArray getCRMMenuListByJSON() throws DocumentException,
			FileNotFoundException {
		return JSONArray.fromObject(getCRMMenuList());
	}

	public JSONArray getAreaListByJSON() throws DocumentException,
			FileNotFoundException {
		return JSONArray.fromObject(getAreaList());
	}
}
